﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;

namespace MixScriptWeb.Jideco
{
    public partial class fUpload : cCommonWeb2//System.Web.UI.Page
    {
        private readonly Random randNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    mn = (MainMenu2)this.Master;
                    hideStatusBar();
                }
                
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        private string GeneratedRandomUniqueId(int MaximalSize, string HeaderSignature, params string[] NextCodes)
        {
            if (MaximalSize < 1)
            {
                return HeaderSignature;
            }

            int genSize = (MaximalSize - (string.IsNullOrEmpty(HeaderSignature) ? 0 : HeaderSignature.Length));

            if (genSize < 1)
            {
                return HeaderSignature;
            }

            string codes = string.Empty;
            char[] chars = ("1234567890abcdef").ToCharArray();
            byte[] datas = null;

            if ((NextCodes != null) && (NextCodes.Length > 0))
            {
                codes = string.Join(string.Empty, NextCodes);
                datas = Encoding.UTF8.GetBytes(codes);
            }
            else
            {
                datas = new byte[4];
                randNumber.NextBytes(datas);
            }


            System.Security.Cryptography.RNGCryptoServiceProvider crypto = new System.Security.Cryptography.RNGCryptoServiceProvider();

            crypto.GetNonZeroBytes(datas);
            datas = new byte[genSize];
            crypto.GetNonZeroBytes(datas);

            StringBuilder result = new StringBuilder(MaximalSize);

            result.Append(HeaderSignature);
            //foreach (byte b in data)
            for (int nLoop = 0; nLoop < datas.Length; nLoop++)
            {
                result.Append(chars[datas[nLoop] % (chars.Length - 1)]);
            }

            if (result.Length > MaximalSize)
            {
                result.Remove(MaximalSize, (result.Length - MaximalSize));
            }
            else if (result.Length < MaximalSize)
            {
                while (result.Length > MaximalSize)
                {
                    result.Append(chars[randNumber.Next(0, (chars.Length - 1))]);
                }
            }

            return result.ToString();
        }

       
        private   string  genNewRndFileName(out string sid)
        {
          //  string s = "";
            sid = "";
            try
            {
                bool bfound = true;                  
                while (bfound)
                {
                    sid = GeneratedRandomUniqueId(50, "", new string[] { "jgk3984y9", "8yr9n3nr9", "082704827b982" });
                    cUpload oUpload = new cUpload();
                    bfound = oUpload.isRecordExist("tb_upload", new string[] { "FILE_UPLOADED" }, new string[] { sid });
                }
                return sid;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                
                string sid = "";
                if (ddlFileType.SelectedIndex > 0)
                {
                    if (flUpload.HasFile)
                    {
                        flUpload.SaveAs(Server.MapPath("../upload/") + genNewRndFileName(out sid));
                    }

                    cUpload oUpl = new cUpload();
                    oUpl.saveUploadLog(sid, flUpload.FileName, getUserLogin(), Convert.ToInt16(ddlFileType.SelectedValue));
                    ShowStatusBar("Upload Success", msgType.msgSuccess);
                }
                else
                {
                    ShowStatusBar("Please Select Type of file first");
                }
               
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        private bool isvalid()
        {

            return true;
        }
    }
}